package com.tozed.test;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.cmcc.nativepackage.MagCard;
import com.tozed.widget.TitleBar;
import com.tozed.util.Util;

public class MagneticCardTestActivity extends Base implements
		OnClickListener
{
	private TitleBar mTitleBar;
	private Button mBtnPass, mBtnFail;
	private TextView mTvCardInfo;
	private boolean mExit = false;
	
	private final static int MSG_OPEN_DEV_FAILED = 1;
	private final static int MSG_READ_CARD = 2;
	
	public Handler mHandler = new Handler() {
		
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_OPEN_DEV_FAILED:
				mTvCardInfo.setText(R.string.str_dev_open_fail);
				break;
			case MSG_READ_CARD:
				byte[] first, second, third;
				String content;
				
				first = new byte[110];
				second = new byte[110];
				third = new byte[110];
				
				if (MagCard.getBankCardInfo(first, second, third) == 0) {
					content = Util.getResString(R.string.str_track_1) + first + "\n";
					content += Util.getResString(R.string.str_track_2) + second + "\n";
					content += Util.getResString(R.string.str_track_3) + third + "\n";
					mTvCardInfo.setText(content);
				}
				
				break;
			}
			String str = null; //= MagCard.Read();
			
			if (str != null && !str.isEmpty()) {
				mTvCardInfo.setText(parseData(str));
			}
		};
	};
	
	public Runnable mRunnable = new Runnable() {
		
		@Override
		public void run() {
			
			while (!mExit) {
				mHandler.sendEmptyMessage(MSG_READ_CARD);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.magnetic_card_test);
		
		mTitleBar = (TitleBar)findViewById(R.id.title_bar);
		mTitleBar.setTiteText(R.string.str_test_magneticcard);
		mTitleBar.hideLeftButton();
		mTitleBar.hideRightButton();	
		
		mBtnPass = (Button)findViewById(R.id.btn_pass);
		mBtnPass.setOnClickListener(this);
		mBtnFail = (Button)findViewById(R.id.btn_fail);
		mBtnFail.setOnClickListener(this);
		
		mTvCardInfo = (TextView)findViewById(R.id.mag_card_info);
		
		if (MagCard.intopenBankCard(4, "", "") != 0)
		{
			mHandler.sendEmptyMessage(MSG_OPEN_DEV_FAILED);
		}
		
		MagCard.intinitialBankCard();
		new Thread(mRunnable).start();
	}
	
	@Override
	protected void onDestroy() {
		mExit = true;
		super.onDestroy();
	}
	
	public String parseData(String data) {
		String first = Util.getResString(R.string.str_track_1);
		String second = Util.getResString(R.string.str_track_2);
		String third = Util.getResString(R.string.str_track_3);
		String result = null;
		int start = 0, end, i;
		int flag = 0;
		
		for (i = 0; i < data.length(); i++) {
			switch (data.charAt(i)) {
			case '%':
				start = i + 1;
				flag = 1;
				break;
			case ';':
				start = i + 1;
				if (flag == 2) {
					flag = 3;
				} else {
					flag = 2;
				}
				break;
			case '?':
				end = i;
				if (flag == 1) {
					first += data.substring(start, end);
				} else if (flag == 2) {
					second += data.substring(start, end);
				} else if (flag == 3) {
					third += data.substring(start, end);
				}
				break;
			}
		}
		
		result = first + "\n\n" + second + "\n\n" + third + "\n\n";
		
		return result;
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_pass:
			Intent intent = new Intent();
			intent.putExtra("KEY_TEST_STATUS", 1);
			setResult(RESULT_OK, intent);
			finish();
			break;
			
		case R.id.btn_fail:
			Intent intent2 = new Intent();
			intent2.putExtra("KEY_TEST_STATUS", 0);
			setResult(RESULT_OK, intent2);
			finish();
			break;
		}
	}
}
